from django.contrib import admin
from Nav.models import Nav, Category,ImagesModel
from YaoNav.AdminInfo import site_title,site_header,index_title
from django.utils.html import format_html
from Nav.FilterModel import IsCuteFilter

admin.site.site_header = site_header  # 设置header
admin.site.site_title = site_title   # 设置title
admin.site.index_title = index_title
# Register your models here.

class CategoryAdmin(admin.ModelAdmin):
    list_per_page = 13
    list_filter = ("is_show",)
    search_fields = ('name',)
    search_help_text = "输入分类名称进行搜索"
    list_display = ("id","rank","name", "farther", "icon_name", "itemssum","is_show")
    list_editable = ("rank","name", "farther", "icon_name", "is_show")
    def itemssum(self, obj):
        return obj.nav_set.count()
    itemssum.short_description = "导航数"


class NavAdmin(admin.ModelAdmin):
    search_fields = ('name',"category__name", "description")
    list_filter = ("category__name", "permission")
    list_per_page = 12
    list_display = ("name", "show_image","category", "url", "description","permission")
    list_editable = ("url", "permission")
    def show_image(self, obj):
        if obj.image:
            html = "<img src='%s' width=50em >" % obj.image.image.url
        else:
            html = "<img src='%s' width=50em >"
        return format_html(html)

    show_image.short_description = "网站图标"
    # show_image.admin_order_field = "image"
class ImagesModelAdmin(admin.ModelAdmin):
    list_per_page = 5

    list_display = ("image","show_image", "add_date", "is_cute")
    list_filter = (IsCuteFilter,)
    def show_image(self, obj):
        html = "<img src='%s' width=100em >" % obj.image.url
        return format_html(html)

    def is_cute(self, obj):
        if obj.nav_set.count() == 0:
            return False
        return True
    is_cute.short_description = "是否被引用"
    is_cute.boolean = True
    show_image.short_description = "图片"
    show_image.admin_order_field = "image"



admin.site.register(Nav,NavAdmin)
admin.site.register(Category, CategoryAdmin)

admin.site.register(ImagesModel, ImagesModelAdmin)